package com.slh.touchandroid.fragment;

import java.util.ArrayList;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.slh.touchandroid.R;
import com.slh.touchandroid.utils.AppUtils;

public class RecentAppFragment extends BaseFragment {
	private ArrayList<ApplicationInfo> mApplicationInfos;
	private RecentAppAdapter mAppAdapter;
	private ListView mAppListView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		setRetainInstance(true);
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater,
			 ViewGroup container,  Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_list, container,
				false);
		mAppListView = (ListView) rootView
				.findViewWithTag(getString(R.string.tag_list));
		loadApps();
		return rootView;
	}

	// ===================================================================
	private void loadApps() {
		new AsyncTask<Void, Void, ArrayList<ApplicationInfo>>() {

			@Override
			protected ArrayList<ApplicationInfo> doInBackground(Void... params) {
				return null;
			}
		};
	}

	// ===================================================================
	private static class Holder {
		TextView tvName;
	}

	// ===================================================================
	private static class RecentAppAdapter extends BaseAdapter {
		private Context context;
		private ArrayList<ApplicationInfo> applications;

		public RecentAppAdapter(Context context,
				ArrayList<ApplicationInfo> applications) {
			super();
			this.context = context;
			this.applications = applications;
		}

		@Override
		public int getCount() {
			return applications.size();
		}

		@Override
		public Object getItem(int position) {
			return applications.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Holder holder;
			if (null == convertView) {
				convertView = LayoutInflater.from(context).inflate(
						R.layout.row_common, null);
				holder = new Holder();
				holder.tvName = (TextView) convertView.findViewWithTag(context
						.getString(R.string.tag_name));
				convertView.setTag(holder);
			} else {
				holder = (Holder) convertView.getTag();
			}
			ApplicationInfo application = applications.get(position);
			holder.tvName.setText(AppUtils.getApplicationLabel(context,
					application));
			holder.tvName.setCompoundDrawablesWithIntrinsicBounds(
					AppUtils.getApplicationIcon(context, application), null,
					null, null);
			return convertView;
		}

	}

}
